﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.MobileServices;
using TestMatrix.BLController;
using TestMatrix.Model;

namespace TestMatrix.Phone
{
    public class TodoItem
    {
        public string Id { get; set; }

        [JsonProperty(PropertyName = "text")]
        public string Text { get; set; }

        [JsonProperty(PropertyName = "complete")]
        public bool Complete { get; set; }
    }

    public partial class TestPage : PhoneApplicationPage
    {

        private IMobileServiceTable<BugListAzure> bugListAzureTable = App.MobileService.GetTable<BugListAzure>();
        private MobileServiceCollection<BugListAzure, BugListAzure> bugItems;

        private MobileServiceCollection<TodoItem, TodoItem> items;
        private IMobileServiceTable<TodoItem> todoTable = App.MobileService.GetTable<TodoItem>();

        public TestPage()
        {
            InitializeComponent();
            //SqliteTest();
            getBugListFromWindowAzure();
        }

        private void SqliteTest()
        {
            //string strSeries = "select distinct Severity from tblBugList order by Severity";

            string strSeries = "select count(1) As Count from tblBugList";

            List<tblBugList> listSeries = ChartController.Query(strSeries);

            int num = listSeries.Count;
        }

        private async void getBugListFromWindowAzure()
        {
            List<BugListAzure> lstBugs = new List<BugListAzure>();

            try
            {
                //items = await todoTable
                // .Where(todoItem => todoItem.Complete == false)
                // .ToCollectionAsync();

                int takeCount = 50;
                int skipCount = 0;
                while (1 == 1)
                {
                    IMobileServiceTableQuery<BugListAzure> query = bugListAzureTable.Skip(skipCount).Take(takeCount);
                    List<BugListAzure> lstTmp = await query.ToListAsync();

                    for (int i = 0; i < lstTmp.Count; i++)
                    {
                        lstBugs.Add(lstTmp[i]);

                        ChartController.InsertBugs(new tblBugList(
                            lstTmp[i].Id
                            , lstTmp[i].ItemID
                            , lstTmp[i].ProjectName
                            , lstTmp[i].AssignedTo
                            , lstTmp[i].State
                            , lstTmp[i].Severity
                            , lstTmp[i].FoundInEnvironment
                            , lstTmp[i].CreatedDate
                            , lstTmp[i].ResolvedDate
                            , lstTmp[i].ClosedDate
                            , lstTmp[i].ProjectFriendName
                            , lstTmp[i].BugType
                            , lstTmp[i].CreatedBy
                            , lstTmp[i].ResolvedBy
                            , 0
                            ));
                    }

                    if (lstTmp.Count < takeCount)
                    {
                        break;
                    }

                    skipCount = skipCount + takeCount;
                }

                int totalCount = lstBugs.Count;

                
            }
            catch (MobileServiceInvalidOperationException e)
            {
                MessageBox.Show(e.Message, "Error loading items", MessageBoxButton.OK);
            }
        }
    }
}